package com.quyunshuo.module.home.fragment.compose

import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.quyunshuo.module.home.R
import com.quyunshuo.module.home.activity.main.SharedViewModel
import com.quyunshuo.module.home.music.Music
import com.quyunshuo.module.home.music.MusicPlayer


@Composable
fun LocalMusicPage(
    musics: List<Music>,
    vm:SharedViewModel= viewModel(),//必须要传递，不然用的不是同一个实力，监听不到数据回来。
    onPlay: (Music,Int) -> Unit,
) {
    // 使用可观察状态监听播放器变化
    val currentMusic by vm.currentMusic.collectAsState()
    val isPlaying by vm.isPlaying.collectAsState()


    LazyColumn {
        itemsIndexed(musics) {index,it->
            // 判断当前歌曲是否正在播放
            val isCurrentPlaying = currentMusic?.id == it.id  && isPlaying
            Row(
                modifier = Modifier
                    .fillMaxSize()
                    .height(60.dp)
                    .border(
                        border = BorderStroke(
                            width = 0.5.dp, // 边框宽度
                            color = Color.White // 边框颜色
                        ),
                        shape = RoundedCornerShape(12.dp) // 圆角形状
                    )
                    .padding(start = 10.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Image(
                    painterResource(R.drawable.setting_icon),
                    contentDescription = null,
                    modifier = Modifier.size(40.dp)
                )
                Spacer(Modifier.width(5.dp))
                Column {
                    Text(
                        it.title, color = Color(0xffffffff),
                        modifier = Modifier.width(200.dp), // 重要：约束文本宽度
                        maxLines = 1,                      // 限制为单行
                        overflow = TextOverflow.Ellipsis   // 超出部分显示省略号
                    )
                    Text(
                        it.title, fontSize = 12.sp, color = Color(0xff33ffffff),
                        modifier = Modifier.width(200.dp), // 重要：约束文本宽度
                        maxLines = 1,                      // 限制为单行
                        overflow = TextOverflow.Ellipsis   // 超出部分显示省略号
                    )

                }
                Spacer(modifier = Modifier.weight(1f))
                Text(MusicPlayer.formatDuration(it.duration), color = Color(0xff33ffffff))
                Spacer(modifier = Modifier.width(5.dp))
                Image(
                    painterResource(if (isCurrentPlaying) R.drawable.stop else R.drawable.start),
                    contentDescription = null,
                    modifier = Modifier
                        .size(20.dp)
                        .clickable {
                            onPlay.invoke(it,index)

                        })
                Spacer(modifier = Modifier.width(10.dp))


            }
            Spacer(Modifier.height(10.dp))
        }
    }
}

@Preview
@Composable
fun LocalMusicPagePreview() {
    LocalMusicPage(listOf(Music(), Music())) {index,it->}
}